package com.dylearn.utils;


import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.ooxml.extractor.POIXMLTextExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class ReadWorld {

    /**
     * world文件导入
     */
    public static String getInfo(String path) {
        URL url = null;
        try {
            url = new URL(path);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }

        URLConnection conn = null;
        try {
            conn = url.openConnection();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        String result = "";
        //首先判断文件中的是doc/docx
        try {
            if (path.endsWith(".doc")) {
                InputStream is = new FileInputStream(path);
                WordExtractor extractor = new WordExtractor(is);
                result = extractor.getText();

                //输出word文档所有的文本
                System.out.println(extractor.getText());
                System.out.println("=================1=================");
                System.out.println("==================2================");
                //                //输出页脚的内容
                System.out.println("页脚：" + extractor.getDocument());
                //                System.out.println("===============4===================");
                //                //输出当前word文档的元数据信息，包括作者、文档的修改时间等。
                System.out.println(extractor.getMetadataTextExtractor().getText());
                System.out.println("===============5===================");
                //获取各个段落的文本
                String paraTexts[] = extractor.getParagraphText();
                for (int i = 0; i < paraTexts.length; i++) {
                    System.out.println("Paragraph " + (i + 1) + " : " + paraTexts[i]);
                }
                //输出当前word的一些信息
                System.out.println(extractor.getTextFromPieces());
                System.out.println("=============6=====================");
                //输出当前word的一些信息
                System.out.println(extractor.getMetadataTextExtractor());
                System.out.println("===============7===================");
                System.out.println(extractor.getEndnoteText());
                System.out.println("===============8===================");


                extractor.close();
            } else if (path.endsWith(".docx")) {
                // 获取流
                InputStream in = conn.getInputStream();
                OPCPackage opcPackage = OPCPackage.open(in);
                POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
                result = extractor.getText();

                extractor.close();
            } else {
                System.out.println("此文件不是word文件");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }
}
